package cc.iotkit.screen.dao;

import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.screen.dto.Period;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Map;

/***
 * 工作排名
 */
@Component
public class JobRankingRepositoryImpl implements JobRankingRepository{
    @PersistenceContext
    private EntityManager entityManager;

    /**
     * category：配送/消杀/扫地/洗地/接待
     * 示例：
     * 年：/screen/job_ranking/year/2024/配送
     * 月：/screen/job_ranking/month/2024-06/配送
     * 日：/screen/job_ranking/day/2024-07-21/配送
     * @param period
     * @param date
     * @param category
     * @return
     */
    public List<Map<String,Object>> getStatistics(String period,String date, String category){
        int limit=10;
        String sql="SELECT a.device_id,min(b.device_name)device_name,min(e.`name`)model,min(c.name)product_name,round(sum(%s),0) as total\n" +
                "FROM `device_daily_report` a\n" +
                "inner join device_info b on a.device_id=b.device_id\n" +
                "inner join product c on b.product_key=c.product_key\n" +
                "inner join category d on c.category=d.id\n" +
                "left join product_model e on e.id=b.model_id\n" +
                "WHERE d.`name`='"+category+"' and %s\n" +
                "GROUP BY device_id\n" +
                "ORDER BY total desc\n" +
                "limit %s\n";
        String where=tenantPermission();
        String countField=getCountField(category);
        if(period.equals(Period.Type.year.toString())){
            sql=String.format(sql,countField,"year(report_date)="+date+where,limit);
        }else if(period.equals(Period.Type.month.toString())){
            sql=String.format(sql,countField,"DATE_FORMAT(report_date,'%Y-%m')='"+date+"'"+where,limit);
        }else if(period.equals(Period.Type.day.toString())){
            sql=String.format(sql,countField,"DATE_FORMAT(report_date,'%Y-%m-%d')='"+date+"'"+where,limit);
        }
        List<Map<String,Object>> result= entityManager.createNativeQuery(sql).unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
                .getResultList();
        return result;
    }

    private String getCountField(String category){
        if(category.equals("配送")){
            return "distribution_times";
        }else if(category.equals("消杀")){
            return "online_times";
        }else if(category.equals("扫地")){
            return "square";
        }else if(category.equals("洗地")){
            return "square";
        }else if(category.equals("接待")){
            return "receive_times";
        }
        return null;
    }
    private String tenantPermission(){
        //超级管理员或系统进程调用
        if(LoginHelper.isSuperAdmin()|| RequestContextHolder.getRequestAttributes()==null)
            return "";
        else {
            return " and b.tenant_id="+LoginHelper.getTenantId();
        }
    }
}
